const getKey = (key, list = []) => {
	const item = list.filter(item => item.field == key)[0]
	return item ? item.value : ''
}

export function getComponentsName(name, type = 'el-') {
	return type + name
}

export function makeRequiredRule() {
	return {
		type: 'switch',
		field: '$required',
		title: '是否必填'
	};
}
/**
 * @param {Object} to 类型
 */
export function makeOptionsRule(to) {
	const options = [{
			'label': '动态数据',
			'value': 0
		},
		{
			'label': '静态数据',
			'value': 1
		},
	];

	const control = [{
			value: 0,
			rule: [{
				type: 'Fetch',
				field: 'Api',
				props: {
					defaultValue: []
				}
			}, ],
		},
		{
			value: 1,
			rule: [{
				type: 'TableOptions',
				field: 'fetch',
				props: {
					to,
				}
			}]
		}
	];

	return {
		type: 'radio',
		title: '选项数据',
		field: '_optionType',
		value: 0,
		options,
		props: {
			type: 'button'
		},
		control
	};
}

export function getComponentsProp(item) {
	// console.log(item.rule())
	const rule = item.rule() || {}
	const props = item.props() || []
	const prop = {}
	
	if (rule.props){
		for(var key in rule.props){
			prop[key]=rule.props[key]
		}
	}
	
	props.forEach(v => {
		if(v.type=='switch'){
			v.value=v.value || (v.props?v.props.inactiveValue:false)
		}
		prop[v.field] = v.value ||  (v.props?v.props.defaultValue:undefined)
	})
	// console.log(rule)
	if (rule.options) prop.options = rule.options
	// console.log(prop)
	return prop
}